/**   
 * @Title: Splash.java 
 * @Package com.esun.cheetah 
 * @Description: 加载页
 * @author 程相亭 <br/>
 * @date 2012-03-13 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.esun.cheetah.biz.GetTimeCommunicater;
import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater;
import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater.UpgradeInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.view.HomeAct;
import com.esun.cheetah.view.introduce.IntroActivity;
import com.esun.cheetah.view.setting.ServiceManager;
import com.esun.util.TimeUtil;
import com.esun.util.VersionUtils;

/**
 * com.esun.cheetah.Splash
 * 
 * @Description:加载页面
 * @author 程相亭 <br/>
 * @date 2012-3-13 下午05:51:35
 */
public class Splash extends Activity {

	private Handler mGetTimeHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 锁定横屏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.splash);

		mGetTimeHandler = new GetTimeHandler();
		getRemoteDate();
		checkUpgrade();
		openNotice();
	}

	/**
	 * @title checkUpgrade
	 * @description 检查更新
	 */
	private void checkUpgrade() {
		Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if (msg.what == Config.COMMUNICATER_SUCCESS) {
					VersionUtils.getInstance().setActived(Splash.this);
					UpgradeInfo info = (UpgradeInfo) msg.obj;
					VersionUtils.getInstance().setmMustUpgrade(true) ;
					if (info.isUpgrade) {
						VersionUtils.getInstance().setNeedUpgrade(true);
						VersionUtils.getInstance().setUpgradeInfo(info);
					}
				}
				
				if (isFirstRunning()) {
					startActivity(new Intent().setClass(Splash.this,
							IntroActivity.class));
				} else {
					startActivity(new Intent().setClass(Splash.this,
							HomeAct.class));
				}
				Splash.this.finish();
			}

		};
		final String channel = VersionUtils.getInstance().getChannel(this);
		final int version = VersionUtils.getInstance().getVersion(this);
		final int isJiHuo = VersionUtils.getInstance().getIsActive(this);
		final String sysVersion = Build.VERSION.RELEASE;
		final String screenreso = VersionUtils.getInstance().getScreenreso(this);
		final String serial = VersionUtils.getInstance().getSerial(this);
		
		
		if (TextUtils.isEmpty(channel) || version < 0) {
			return;
		}
		CheckUpgradeCommunicater checkUpgradeCommunicater = new CheckUpgradeCommunicater(handler,this, version, channel, sysVersion, screenreso, serial, isJiHuo);
		checkUpgradeCommunicater.startCommunicate();

	}

	/**
	 * @title getRemoteDate
	 * @description 获得服务器时间
	 */
	private void getRemoteDate() {
		GetTimeCommunicater getTimeCommunicater = new GetTimeCommunicater(
				mGetTimeHandler);
		getTimeCommunicater.startCommunicate();
	}

	/**
	 * @title GetTimeHandler
	 * @description
	 * @author liugan
	 */
	class GetTimeHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				// 将时间更新到TimeUtil类
				TimeUtil.getInstance().checkForRemoteDateChange(
						(String) msg.obj);
				break;
			case Config.COMMUNICATER_FAIL:
			case Config.COMMUNICATER_EXCEPTION:

				break;
			}
		}

	}

	/**
	 * @title isFirstRunning
	 * @description 判断是否第一次运行
	 * @return
	 */
	private boolean isFirstRunning() {
		try {
			// 保存了最近版本的SharedPreferences
			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			// 当前版本
			final String currentVersionName = this.getPackageManager()
					.getPackageInfo(Config.APP_PACKAGE_INFO, 0).versionName;
			// 最近保存的版本信息
			final String lastSavedVersionName = preferences.getString(
					Config.SAVED_VERSION, "");
			// 判断如果不一致则需要显示介绍页面
			if (TextUtils.isEmpty(lastSavedVersionName)
					|| (!lastSavedVersionName.equals(currentVersionName))) {
				SharedPreferences.Editor editor = preferences.edit();
				editor.putString(Config.SAVED_VERSION, currentVersionName);
				editor.commit();
				return true;
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}
	/**
	 * 开启push通知
	 */
	private void openNotice() {
		ServiceManager serviceManager = new ServiceManager(this);
		serviceManager.startService();

	}
}